﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Sharp.DataTransferObject;
using Sharp.DataTransferObject.CMS;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Communication;
using Sharp.Platform.Mvc;
using Sharp.ServiceContracts;
using Sharp.ServiceContracts.CMS;
using Sharp.WebBooter.Areas.CMS.Models;

namespace Sharp.WebBooter.Areas.CMS.Controllers
{
    public class UserController : Controller
    {

        [HttpGet]

        public ActionResult Index()
        {
            using (var proxy = new ServiceProxy<IUserService>())
            {
                var model = new UserIndexViewModel
                {
                    UserGroups = proxy.Channel.GetUserGroups().Data.ToList()
                };
                return View(model);
            }
        }

        /// <summary>
        /// 用户分页数据
        /// </summary>
        /// <param name="param"></param>
        /// <param name="userGroupId">用户组Id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetPaged(DataTableParameter param, Guid? userGroupId)
        {
            using (var proxy = new ServiceProxy<IUserService>())
            {

                var page = proxy.Channel.GetUsersPaged(param.ToPagingFeature(), userGroupId);

                return this.Store(page);
            }
        }


        [Permission(Name = "新增", Order = 1)]
        public ActionResult Create()
        {
            using (var proxy = new ServiceProxy<IUserService>())
            {
                var model = new UserViewModel
                {
                    UserGroupIds = new Guid[] { },
                    UserGroupList = proxy.Channel.GetUserGroups().Data
                };
                ViewBag.EditUserNameAble = true; // 是否可以编辑用户名

                return View("Edit", model);
            }
        }


        [Permission(Name = "编辑", Order = 2)]
        public ActionResult Edit(Guid id)
        {
            using (var proxy = new ServiceProxy<IUserService>())
            {
                var user = proxy.Channel.GetUserByKey(id).Data;
                var model = user.To<UserViewModel>();
                model.Password = "";
                model.UserGroupList = proxy.Channel.GetUserGroups().Data;
                model.UserGroupIds = user.UserGroups.Select(x => x.Id);
                ViewBag.EditUserNameAble = false; // 是否可以编辑用户名

                return View("Edit", model);
            }
        }


        [HttpPost]
        public ActionResult Save(UserDTO vm)
        {
            if (vm == null)
            {
                return this.JsonNet(new Result() { Err = "提交数据出错。" });
            }
            using (var proxy = new ServiceProxy<IUserService>())
            {

                var res = proxy.Channel.SaveUser(vm);

                return this.JsonNet(res);
            }
        }

        [HttpPost]
        [Permission(Name = "删除", Order = 3)]
        public ActionResult Delete(Guid[] ids, FormCollection collection)
        {
            using (var proxy = new ServiceProxy<IUserService>())
            {
                var res = proxy.Channel.DeleteUsers(ids);
                return this.JsonNet(res);
            }
        }

        public ActionResult MemberSetting()
        {
            return View();
        }
        public ActionResult OAuth()
        {
            return View();
        }


        [HttpPost]
        public ActionResult GetOauthList()
        {
            using (var proxy = new ServiceProxy<IMemberService>())
            {

               Result<IEnumerable<OAuthDTO>> result = proxy.Channel.GetOauthList();

                return this.JsonNet(result.Data);
            }
        }

        [HttpPost]
        public ActionResult SaveOauthList(IEnumerable<OAuthDTO> dtoModelList )
        {
            if (dtoModelList==null)
            {
                return  Json(new Result() { Err = "dtoModelList 不能为空" });
            }
            using (var proxy = new ServiceProxy<IMemberService>())
            {
                foreach (var dto in dtoModelList)
                {
                    var res = proxy.Channel.SaveOauth(dto);
                }
            }
             return this.JsonNet(new Result()); 
        }
    }
}